const Base = require('./base.js');
const moment = require('moment');

module.exports = class extends Base {
    /**
     * 积分详情
     * @returns {Promise<void>}
     */
    async detailsAction() {
        let d = this.post();
        // const list = await this.model('points').where({user_id: think.user.user_id}).page(d.page || 1, d.limit || 30).order('add_time desc').countSelect();
        const list = await this.model('points').where({user_id: think.user.user_id}).order('add_time desc').select();
        const user_points = await this.model('user').where({id: think.user.user_id}).getField('integral', true);
        return this.success({list: list, points: user_points});
    }

    /**
     * 分享好友送积分
     * @returns {Promise<any | void>}
     */
    async shareAction() {
        const data = await this.model('points').field('points_after,add_time').where({user_id: think.user.user_id}).order('add_time DESC').find();
        if (think.isEmpty(data)) {
            const user_points = await this.model('points').commission(50, '分享好友送积分');
            return this.success(user_points);
        }
        let add_time = moment(data.add_time).format('YYYY-MM-DD');
        if (moment().diff(add_time, 'days') === 0) {
            return this.success(data.points_after);
        }
        const user_points = await this.model('points').commission(50, '分享好友送积分');
        return this.success(user_points);
    }
};
